﻿using System;
using System.Windows.Forms;
using System.IO;


namespace Nathan_tools
{
    public partial class frm_file_rename : Form
    {
        public frm_file_rename()
        {
            InitializeComponent();
        }

        private void frm_file_rename_Load(object sender, EventArgs e)
        {
        }

        private void btn_add_foler_Click(object sender, EventArgs e)
        {
            if (DialogResult .OK == folderBrowserDialog1.ShowDialog ())
            {
                DirectoryInfo dr_info = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
                foreach (FileInfo s_file in dr_info.GetFiles())  
                {
                   lb_files.Items.Add(s_file.FullName);
                }
            }
        }

        private void btn_do_Click(object sender, EventArgs e)
        {
            if (lb_files.Items.Count == 0)
            {
                MessageBox.Show("清添加要进行更名的文件！","提示");
                return;
            }
            
            int start_num = Convert.ToInt16(tb_start_num.Text);    //序列化命名起始值
            FileInfo fi = null;
            string file_name = "";

              for (int i = 0; i < lb_files.Items.Count; i++)
                {
                    lb_files.SelectedIndex = i;
                    fi = new FileInfo(lb_files.Text);

                    if (rb__serial.Checked == true)       //序列化命名
                    {
                        file_name = tb_newname_regular_expression.Text.Trim();  //get "y(*)"

                        string str_len = "";
                        for (int j = 0; j < Convert.ToInt16(tb_epr_length.Text); j++) str_len += "0";   // get file serial number length: 3 to "000"

                        file_name = fi.DirectoryName + "\\" + file_name.Replace("(*)", start_num.ToString(str_len)) + fi.Extension;
                        start_num++;
                    }
                    else if (rb_str_replace.Checked == true)   //字符替换命名
                    {
                        file_name = lb_files.Text.Replace(tb_old.Text, tb_new.Text);
                    }

                    try
                    {
                        if (chb_remain_old.Checked != true)
                        {
                            fi.MoveTo(file_name);            //覆盖
                        }
                        else
                        {
                            fi.CopyTo(file_name, true);            //另存
                        }
                        lb_files.Items[lb_files .SelectedIndex ] = file_name;
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show("出现故障！原因是：" + er.Message);
                    }
                }
                MessageBox.Show("更名任务完成！");
  
        }

        private void btn_file_clear_Click(object sender, EventArgs e)
        {
            lb_files.Items.Clear();
        }

        private void tb_old_TextChanged(object sender, EventArgs e)
        {
            rb_str_replace.Checked = true;
        }

        private void tb_newname_regular_expression_TextChanged(object sender, EventArgs e)
        {
            rb__serial.Checked = true;
        }

        private void lb_files_SelectedIndexChanged(object sender, EventArgs e)
        {
           // FileInfo fi = new FileInfo(lb_files.Text);
            //tb_filename.Text = fi.Name ;
        }

        private void btn_add_onfile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
               foreach (string f in openFileDialog1 .FileNames )
                lb_files.Items.Add(f);
            }
        }

        private void lb_files_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            tb_filename.Text = lb_files.Text;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://94011.info");
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lb_files.Items.Remove(lb_files.SelectedItem);
        }
    }
}
